package com.ylx.swagger.config;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;


/**
 * @author PTG
 */
@Configuration
@EnableSwagger2WebMvc
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {

    @Value("${spring.application.name:whale}")
    private String desc;

    @Bean
    @Order(value = 1)
    //只在dev和test生效
    @Profile({"dev", "test"})
    public Docket createRestApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
        docket.globalResponseMessage(RequestMethod.GET, getGlobalResponseMessage());
        docket.globalResponseMessage(RequestMethod.POST, getGlobalResponseMessage());
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .contact(new Contact("yanglixin","8.134.214.134","458040194@qq.com"))
                //设置文档的标题
                .title(desc)
                // 设置文档的描述
                .description(desc + " API 接口文档")
                // 设置文档的版本信息-> 1.0.0 Version information
                .version("V1.0.0")
                .build();
    }

    private List<ResponseMessage> getGlobalResponseMessage() {
        List<ResponseMessage> responseList = new ArrayList<>();
        responseList.add(new ResponseMessageBuilder().code(HttpStatus.OK.value()).message("访问成功").build());
        responseList.add(new ResponseMessageBuilder().code(HttpStatus.UNAUTHORIZED.value()).message("会话未能通过登录认证").build());
        responseList.add(new ResponseMessageBuilder().code(HttpStatus.FORBIDDEN.value()).message("会话未能通过权限认证").build());
        responseList.add(new ResponseMessageBuilder().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("网络繁忙").build());
        return responseList;
    }
}

